package com.zygh.policeasset.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.fastjson.JSONObject;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.listenter.PurchaseQrCodeInfoListener;
import com.zygh.policeasset.mapper.PurchaseQrCodeInfoMapper;
import com.zygh.policeasset.pojo.PurchaseCodeGenerated;
import com.zygh.policeasset.pojo.PurchaseCodeInfo;
import com.zygh.policeasset.service.DailyProjectService;
import com.zygh.policeasset.service.PurchaseCodeGeneratedService;
import com.zygh.policeasset.service.PurchaseService;
import com.zygh.policeasset.service.SettlementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping({"/purchaseCodeGenerated"})
@Api(tags = {"采购项目二维码生成"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/PurchaseCodeGeneratedController.class */
public class PurchaseCodeGeneratedController {
    @Autowired
    PurchaseQrCodeInfoMapper purchaseQrCodeInfoMapper;
    @Autowired
    SettlementService settlementService;
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    DailyProjectService dailyProjectService;
    @Autowired
    PurchaseCodeGeneratedService purchaseCodeGeneratedService;

    @RequestMapping(value = {"/getQrCode"}, method = {RequestMethod.POST})
    @ApiOperation("添加需生成二维码的项目")
    public Object push(@RequestBody PurchaseCodeGenerated purchaseCodeGenerated) {
        if (this.purchaseCodeGeneratedService.selectCountByBuildNum(purchaseCodeGenerated.getBuildNum()).intValue() > 0) {
            return new CommonResult().validateFailed("此项目已生成过二维码");
        }
        if (this.purchaseCodeGeneratedService.add(purchaseCodeGenerated).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加失败");
    }

    @RequestMapping(value = {"/findAll"}, method = {RequestMethod.GET})
    @ApiOperation("二维码信息查询")
    public Object findAll(int pageNum, int pageSize, @RequestParam(value = "buildName", required = false) String buildName, @RequestHeader("findAll") boolean findAll, @RequestHeader("unitId") Integer unitId) {
        if (findAll) {
            return this.purchaseCodeGeneratedService.selectList(pageNum, pageSize, buildName, null);
        }
        return this.purchaseCodeGeneratedService.selectList(pageNum, pageSize, buildName, unitId);
    }

    @RequestMapping(value = {"/selectListById"}, method = {RequestMethod.GET})
    @ApiOperation("通过项目信息主键查询二维码信息")
    public Object selectListById(Integer id) {
        return this.purchaseCodeGeneratedService.selectListById(id);
    }

    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    @ApiOperation("资产二维码删除")
    public Object findAll(@RequestBody JSONObject json) {
        if (this.purchaseCodeGeneratedService.delete(json.getInteger("id")).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/selectProject"}, method = {RequestMethod.GET})
    @ApiOperation("采购项目查询")
    public Object selectProject(@RequestParam(value = "name", required = false) String name, @RequestHeader("findAll") boolean findAll, @RequestHeader("unitId") Integer unitId) {
        if (findAll) {
            return this.purchaseCodeGeneratedService.selectProject(name, null);
        }
        return this.purchaseCodeGeneratedService.selectProject(name, unitId);
    }

    @RequestMapping(value = {"/exportQrCode"}, method = {RequestMethod.GET})
    @ApiOperation("资产二维码信息导出")
    public Object exportQrCode(@RequestParam("id") Integer id, HttpServletResponse response) {
        return this.purchaseCodeGeneratedService.selectQrCode(id, response);
    }

    @RequestMapping(value = {"/importQrCode"}, method = {RequestMethod.POST})
    @ApiOperation("资产信息导入")
    public Object exportQrCode(@RequestBody MultipartFile file, @RequestParam("qrCodeGeneratedId") Integer qrCodeGeneratedId) throws IOException {
        if (this.purchaseCodeGeneratedService.selectListById(qrCodeGeneratedId).size() > 0) {
            return new CommonResult().validateFailed("此项目已生成过二维码");
        }
        try {
            EasyExcel.read(file.getInputStream(), PurchaseCodeInfo.class, new PurchaseQrCodeInfoListener(qrCodeGeneratedId)).sheet().doRead();
            return new CommonResult().success();
        } catch (ExcelAnalysisException e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.getCause().getMessage());
        } catch (ConstraintViolationException e2) {
            e2.printStackTrace();
            return new CommonResult().validateFailed(e2.getMessage());
        }
    }
}